import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

import java.util.ArrayList;
import java.util.HashMap;

public class Baidu_supic extends Thread{
    static Init init = new Init();
    WebDriver chrome;
    int n;
    public Baidu_supic (WebDriver chrome, int n){
        this.chrome=chrome;
        this.n=n+1;
    }
    public void run(){
        System.out.println("启动线程:"+Thread.currentThread().getName()+",开始加载页面数据");
        JavascriptExecutor js = (JavascriptExecutor)chrome;
        for (int i=1;i<=n;i++){
            for (int j = 5000*i-4999;j<=5000*i;j+=1){
                js.executeScript("scrollTo(0,"+j+")");
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
//            js.executeScript("scrollTo(0,"+5000*i+")");
//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
        String url = chrome.getPageSource();
        chrome.quit();
        ArrayList<String> list = new ArrayList<>();
        String[] str = url.split("objURL");
        for (int j = 1; j<str.length;j++){
            String s = str[j].split(",")[0];
            list.add(s.substring(3,s.length()-1));
        }
        String[] str1 = url.split("data-objurl");
        for (int j=1;j<str1.length;j++){
            String s = str1[j].split("\"")[1];
            list.add(s);
            System.out.println(s);
        }
        System.out.println("线程:"+Thread.currentThread().getName()+"页面数据加载完毕,跳转到下载线程");
        HashMap<String,String> header = init.Builder(Main.urls[0]);
        Thread thread = new Thread(new Baidu_pic(list,header));
        thread.start();
    }
}
